﻿using DevExpress.Data.Utils;
using HalconDotNet;
using ImageDefine;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ImageToolLine
{
    public partial class FormSetting : DevExpress.XtraEditors.XtraForm
    {
        ImageTask imageTask;
        ImageToolLine imageTool;
        ImageActionerLine imageActioner;
        public FormSetting()
        {
            InitializeComponent();
        }
        public FormSetting(ImageTask imageTaskIn, ImageToolLine imageToolIn, ImageActionerLine imageActionerIn)
        {
            InitializeComponent();
            imageTask = imageTaskIn;
            imageTool = imageToolIn;
            imageActioner = imageActionerIn;
        }

        private void FormSetting_Load(object sender, EventArgs e)
        {
            propertyGridControl1.SelectedObject = imageTool.Setting;
            imageTask.LoadRegeditFile(smartDisplayControl1);

            comboBoxEditTraceName.Text = imageTool.strTraceName;
            foreach (ImageToolBase itemTool in imageTask.listTools)
            {
                if(itemTool is ImageDefine.IAffine)
                    comboBoxEditTraceName.Properties.Items.Add(itemTool.Name);
            }
            if(imageTool.Setting.regionIgnore==null)
            {
                imageTool.Setting.regionIgnore = new HRegion();
                imageTool.Setting.regionIgnore.GenEmptyObj();
            }

        }

        private void comboBoxEditTraceName_SelectedValueChanged(object sender, EventArgs e)
        {
            imageTool.strTraceName= comboBoxEditTraceName.EditValue.ToString();
        }

        private void simpleButton画搜索区域_Click(object sender, EventArgs e)
        {
            if (smartDisplayControl1.ho_Image == null)
                return;
            if (imageTool.Setting.RowCenter == 0)
            {
                smartDisplayControl1.CreateRectangle2();
            }
            else
            {
                smartDisplayControl1.CreateRectangle2((int)imageTool.Setting.RowCenter, (int)imageTool.Setting.ColumnCenter, imageTool.Setting.Phi, (int)imageTool.Setting.Length1, (int)imageTool.Setting.Length2);
            }
            smartDisplayControl1.drawRectangle2Finished += SmartDisplayControl1_drawRectangle2Finished;
        }

        private void SmartDisplayControl1_drawRectangle2Finished(double Row, double Col, double Phi, double length1, double length2)
        {
            smartDisplayControl1.drawRectangle2Finished -= SmartDisplayControl1_drawRectangle2Finished;
            imageTool.Setting.RowCenter= (int)Row;
            imageTool.Setting.ColumnCenter = (int)Col;
            imageTool.Setting.Phi = Phi;
            imageTool.Setting.Length1 = (int)length1;
            imageTool.Setting.Length2 = (int)length2;
            smartDisplayControl1.DispRectangle2(imageTool.Setting.RowCenter,imageTool.Setting.ColumnCenter, imageTool.Setting.Phi, 
                imageTool.Setting.Length1, imageTool.Setting.Length2,"green",true);
            propertyGridControl1.Refresh();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            imageTool.imageTask.LoadRegeditFile(smartDisplayControl1);

            if (imageTool.strTraceName == "")
            {
                
            }
            else
            {
                try
                {
                    //ImageDefine.ImageToolBase toolTrace = ImageTask.ImageTaskManager.taskCenter.dicTasks[strTaskName].dicTools[actionerSettingSelf.strTraceName];

                    ImageDefine.ImageToolBase toolTrace;

                    toolTrace = imageTool.imageTask.dicTools[imageTool.strTraceName];
                    toolTrace.Run(smartDisplayControl1.ho_Image, smartDisplayControl1);


                }
                catch
                {
                   
                }
            }
            imageTool.Run(smartDisplayControl1.ho_Image, smartDisplayControl1);
        }

        private void simpleButtonLoadImage_Click(object sender, EventArgs e)
        {
            imageTool.imageTask.LoadRegeditFile(smartDisplayControl1);
        }

        private void simpleButton画屏蔽区域_Click(object sender, EventArgs e)
        {
            if (smartDisplayControl1.ho_Image == null)
                return;
            if (imageTool.Setting.regionIgnore == null)
            {
                smartDisplayControl1.CreateRectangle2();
            }
            else
            {

                
                double dRowCenter, dColCeter, dPhi, dLength1, dLength2;
                if (imageTool.Setting.regionIgnore.CountObj() > 0)
                {
                    imageTool.Setting.regionIgnore.SmallestRectangle2(out dRowCenter, out dColCeter, out dPhi, out dLength1, out dLength2);
                    smartDisplayControl1.CreateRectangle2((int)dRowCenter, (int)dColCeter, dPhi, (int)dLength1, (int)dLength2);
                }
                else
                {
                    smartDisplayControl1.CreateRectangle2();
                }
            }
            smartDisplayControl1.drawRectangle2Finished += SmartDisplayControl1_drawRectangle2Finished1; ;
        }

        private void SmartDisplayControl1_drawRectangle2Finished1(double Row, double Col, double Phi, double length1, double length2)
        {
            smartDisplayControl1.drawRectangle2Finished -= SmartDisplayControl1_drawRectangle2Finished1; ;
            imageTool.Setting.regionIgnore = new HalconDotNet.HRegion();
            imageTool.Setting.regionIgnore.GenRectangle2(Row, Col, Phi, length1, length2);
            smartDisplayControl1.DispRectangle2(Row, Col, Phi, length1, length2);
        }

        
    }
}
